[XM] Fix stray quotes in usage message in getlabel.py
authoratse@norwich.uk.xensource.com <atse@norwich.uk.xensource.com>
Thu, 28 Sep 2006 11:23:21 +0000 (12:23 +0100)
committeratse@norwich.uk.xensource.com <atse@norwich.uk.xensource.com>
Thu, 28 Sep 2006 11:23:21 +0000 (12:23 +0100)
Fixed built-in function name conflict.
Raise correct exceptions for when option is invalid to be properly
reported by xm.

Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/xen/xm/getlabel.py

index 28e4619f45d240fe942e0aa0015514b20e82a572..f86b798771aaa61e0a3ac3ea004ab78a8c96fc9e 100644 (file)
@@ -25,8 +25,9 @@ from xen.xm.opts import OptionError
 
 def help():
     return """
-    Usage: xm getlabel dom <configfile>"
-           xm getlabel res <resource>\n"
+    Usage: xm getlabel dom <configfile>
+           xm getlabel res <resource>
+           
     This program shows the label for a domain or resource."""
 
 def get_resource_label(resource):
@@ -37,7 +38,7 @@ def get_resource_label(resource):
     try:
         access_control = dictio.dict_read("resources", file)
     except:
-        security.err("Resource label file not found")
+        raise OptionError("Resource label file not found")
 
     # get the entry and print label
     if access_control.has_key(resource):
@@ -45,23 +46,22 @@ def get_resource_label(resource):
         label = access_control[resource][1]
         print "policy="+policy+",label="+label
     else:
-        security.err("Resource not labeled")
+        raise security.ACMError("Resource not labeled")
 
 
 def get_domain_label(configfile):
     # open the domain config file
     fd = None
-    file = None
     if configfile[0] == '/':
         fd = open(configfile, "rb")
     else:
         for prefix in [".", "/etc/xen"]:
-            file = prefix + "/" + configfile
-            if os.path.isfile(file):
-                fd = open(file, "rb")
+            abs_file = prefix + "/" + configfile
+            if os.path.isfile(abs_file):
+                fd = open(abs_file, "rb")
                 break
     if not fd:
-        security.err("Configuration file '"+configfile+"' not found.")
+        raise OptionError("Configuration file '%s' not found." % configfile)
 
     # read in the domain config file, finding the label line
     ac_entry_re = re.compile("^access_control\s*=.*", re.IGNORECASE)
@@ -79,7 +79,7 @@ def get_domain_label(configfile):
 
     # send error message if we didn't find anything
     if acline == "":
-        security.err("Domain not labeled")
+        raise security.ACMError("Domain not labeled")
 
     # print out the label
     (title, data) = acline.split("=", 1)
@@ -89,7 +89,7 @@ def get_domain_label(configfile):
     print data
 
 
-def main (argv):
+def main(argv):
     if len(argv) != 3:
         raise OptionError('Requires 2 arguments')
 
@@ -103,6 +103,11 @@ def main (argv):
         raise OptionError('First subcommand argument must be "dom" or "res"')
 
 if __name__ == '__main__':
-    main(sys.argv)
+    try:
+        main(sys.argv)
+    except Exception, e:
+        sys.stderr.write('Error: %s\n' % str(e))
+        sys.exit(-1)
+